package cn.dgut.training.controller;

import cn.dgut.training.annotation.LoginUser;
import cn.dgut.training.dto.goods.CommentDTO;
import cn.dgut.training.entity.goods.Comment;
import cn.dgut.training.service.impl.CommentServiceImpl;
import cn.dgut.training.vo.BaseVO;
import cn.dgut.training.vo.CommonResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentServiceImpl commentService;


    /**
     * 根据 valueId 显示评论， current、size控制评论的数量。
     * @param
     * @return
     */

    @GetMapping("/list")
    public CommonResponse listComment(@RequestParam(value="valueId", required=false) Integer valueId,
                                      @RequestParam("type") Integer type,
                                      @RequestParam(value="limit", required=false) Integer limit,
                                      @RequestParam(value="page", required=false) Integer page,
                                      @RequestParam(value="showType", required=false) Integer showType,
                                      @RequestParam(value="sort", required=false) String sort,
                                      @RequestParam(value="order", required=false) String order){
        CommentDTO commentDTO = new CommentDTO();
        commentDTO.setValueId(valueId);
        commentDTO.setCommentType(type);
        commentDTO.setSize(limit);
        commentDTO.setCurrent(page);
        commentDTO.setSort(sort);
        commentDTO.setOrderBy(order);
        return CommonResponse.success(commentService.listCommentByPage(commentDTO, showType));
    }


    /**
     * 显示所有评论， current、size控制评论的数量。
     * @param
     * @return
     */

    @GetMapping("/listAllGoods")
    public CommonResponse listAllGoodsComment( @RequestParam(value="limit", required=false) Integer limit,
                                      @RequestParam(value="page", required=false) Integer page,
                                      @RequestParam(value="sort", required=false) String sort,
                                      @RequestParam(value="order", required=false) String order){
        CommentDTO commentDTO = new CommentDTO();
        commentDTO.setValueId(-1);
        commentDTO.setCommentType(0);
        commentDTO.setSize(limit);
        commentDTO.setCurrent(page);
        commentDTO.setSort(sort);
        commentDTO.setOrderBy(order);
        return CommonResponse.success(commentService.listAllGoodsCommentByPage(commentDTO));
    }


    /**
     * 获取评论数量
     * @param
     * @return
     */
    @GetMapping("/count")
    public CommonResponse countComment(@RequestParam("valueId") Integer valueId, @RequestParam("type") Integer type){
        return CommonResponse.success(commentService.countComment(valueId, type));
    }


    /**
     * 创建评论
     * @param commentDTO
     * @return
     */
    @PostMapping("/post")
    public CommonResponse createComment(@LoginUser Long userId, @RequestBody @Valid CommentDTO commentDTO){
//        System.out.println(loginUser);
        commentDTO.setUserId(userId.intValue());
        Comment comment=new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        commentService.save(comment);
        BaseVO<Comment> commentBaseVO = new BaseVO<>();
        List<Comment> commentList=new ArrayList<>();
        commentList.add(comment);
        commentBaseVO.setList(commentList);
        return CommonResponse.success(commentBaseVO);
    }

    @GetMapping("/reply")
    public CommonResponse updateComment(@RequestParam(value="commentId") Integer commentId,
                                        @RequestParam(value="content") String content){
        Comment comment = commentService.getById(commentId);
        comment.setContent(comment.getContent() + "                         商家回复：" + content);
        commentService.updateById(comment);
        return CommonResponse.success(comment);
    }

    @PostMapping("/delete")
    public CommonResponse deleteComment(@RequestBody @Valid Comment comment){
        commentService.removeById(comment.getId());
        return CommonResponse.message();
    }



}
